# Vulkan 性能测试 - 完整命令清单
# 修复路径问题后的正确版本

Write-Host "=== Vulkan 性能分析系统 - 测试流程 ===" -ForegroundColor Cyan
Write-Host ""

# 步骤 1: 编译
Write-Host "步骤 1/5: 编译项目..." -ForegroundColor Yellow
.\hvigorw.bat assembleHap
if ($LASTEXITCODE -ne 0) {
    Write-Host "编译失败!" -ForegroundColor Red
    exit 1
}
Write-Host "✓ 编译成功" -ForegroundColor Green
Write-Host ""

# 步骤 2: 安装
Write-Host "步骤 2/5: 安装应用..." -ForegroundColor Yellow
hdc install entry\build\default\outputs\default\entry-default-signed.hap
if ($LASTEXITCODE -ne 0) {
    Write-Host "安装失败!" -ForegroundColor Red
    exit 1
}
Write-Host "✓ 安装成功" -ForegroundColor Green
Write-Host ""

# 步骤 3: 运行
Write-Host "步骤 3/5: 运行应用..." -ForegroundColor Yellow
Write-Host "启动应用..." -ForegroundColor Cyan
hdc shell aa start -a EntryAbility -b com.huawei.vulkantestapp10

Write-Host ""
Write-Host "应用正在运行..." -ForegroundColor Green
Write-Host "请在设备上操作应用 2-3 分钟 (旋转、缩放等)" -ForegroundColor Yellow
Write-Host ""
Write-Host "按任意键继续停止应用..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# 步骤 4: 停止并导出
Write-Host ""
Write-Host "步骤 4/5: 停止应用并导出数据..." -ForegroundColor Yellow
hdc shell aa force-stop com.huawei.vulkantestapp10
Start-Sleep -Seconds 2
Write-Host "✓ 应用已停止" -ForegroundColor Green

Write-Host ""
Write-Host "拉取性能数据..." -ForegroundColor Cyan

# 拉取汇总数据
Write-Host "  - 拉取 profiling_summary.csv..." -ForegroundColor Gray
hdc file recv /data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/profiling_summary.csv .\oh_profiling_summary.csv

if (Test-Path "oh_profiling_summary.csv") {
    $size = (Get-Item "oh_profiling_summary.csv").Length
    Write-Host "    ✓ 成功 ($size bytes)" -ForegroundColor Green
} else {
    Write-Host "    ✗ 失败" -ForegroundColor Red
    Write-Host ""
    Write-Host "可能的原因:" -ForegroundColor Yellow
    Write-Host "  1. 应用未正确退出" -ForegroundColor Gray
    Write-Host "  2. 性能分析器未启用" -ForegroundColor Gray
    Write-Host "  3. 文件路径错误" -ForegroundColor Gray
    Write-Host ""
    Write-Host "请检查应用日志:" -ForegroundColor Yellow
    Write-Host "  hdc shell hilog | Select-String 'Performance'" -ForegroundColor Cyan
    exit 1
}

# 拉取详细数据
Write-Host "  - 拉取 profiling_detailed.csv..." -ForegroundColor Gray
hdc file recv /data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/profiling_detailed.csv .\oh_profiling_detailed.csv

if (Test-Path "oh_profiling_detailed.csv") {
    $size = (Get-Item "oh_profiling_detailed.csv").Length
    Write-Host "    ✓ 成功 ($size bytes)" -ForegroundColor Green
} else {
    Write-Host "    ⚠ 详细数据文件未找到 (非致命错误)" -ForegroundColor Yellow
}

Write-Host ""

# 步骤 5: 分析
Write-Host "步骤 5/5: 分析性能数据..." -ForegroundColor Yellow
Write-Host ""

if (Test-Path "oh_profiling_summary.csv") {
    Write-Host "运行分析工具..." -ForegroundColor Cyan
    python analyze_performance.py oh_profiling_summary.csv
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host ""
        Write-Host "✓ 分析完成" -ForegroundColor Green
    } else {
        Write-Host ""
        Write-Host "⚠ 分析工具执行出错,但数据文件已成功获取" -ForegroundColor Yellow
    }
} else {
    Write-Host "✗ 无法找到数据文件" -ForegroundColor Red
}

Write-Host ""
Write-Host "=== 测试流程完成 ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "生成的文件:" -ForegroundColor Yellow
if (Test-Path "oh_profiling_summary.csv") {
    Write-Host "  ✓ oh_profiling_summary.csv" -ForegroundColor Green
}
if (Test-Path "oh_profiling_detailed.csv") {
    Write-Host "  ✓ oh_profiling_detailed.csv" -ForegroundColor Green
}

Write-Host ""
Write-Host "下一步:" -ForegroundColor Yellow
Write-Host "  1. 查看分析报告" -ForegroundColor Gray
Write-Host "  2. (可选) 在 Android 平台重复测试" -ForegroundColor Gray
Write-Host "  3. (可选) 运行对比分析:" -ForegroundColor Gray
Write-Host "     python analyze_performance.py oh_profiling_summary.csv android_profiling_summary.csv" -ForegroundColor Cyan
Write-Host ""
